<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class IndexController extends Controller{
    public function index(){
        return view("admin.index");
    }
    // 获取初始化数据
    public function getSystemInit(){
        $homeInfo = [
            'title' => '首页',
            'href'  => '',
        ];
        $logoInfo = [
            'title' => '蓝色家政',
            'image' => 'layui/images/logo.png',
        ];
        $menuInfo = $this->getMenuList();
        $systemInit = [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo,
        ];
        return response()->json($systemInit);
    }

    // 获取菜单列表
    private function getMenuList(){
        $user_id=DB::table('user')->where("username",session('username'))->value('role_id');
        $rows=DB::table('role')->where('id',$user_id)->value('node_id');
        $str=explode(',',$rows);
        if($user_id!=1){
            $menuList=DB::table('node')
                ->where("is_menu","0")
                ->whereIn("id",$str)->get();
        }else{
            $menuList = DB::table('node')
                ->select(['id','pid','title','icon','href'])
                ->where("is_menu","0")
                ->get();
        }
        $menuList = $this->buildMenuChild(0, $menuList);
        return $menuList;
    }

    //递归获取子菜单
    private function buildMenuChild($pid, $menuList){
        $treeList = [];
        foreach ($menuList as $v) {
            if ($pid == $v->pid) {
                $node = (array)$v;
                $child = $this->buildMenuChild($v->id, $menuList);
                if (!empty($child)) {
                    $node['child'] = $child;
                }
                // todo 后续此处加上用户的权限判断
                $treeList[] = $node;
            }
        }
        return $treeList;
    }
   // 服务介绍
    public function introduce(Request $request){
        $page=$request->get('page');
        $limit=$request->get('limit');
        $crows=DB::table('index_introduce')->get();
        $rows=Db::table('index_introduce')->simplePaginate($limit,'*',$limit,$page);
        $data=[
            "code"=>0,
            "msg"=>'',
            "count"=>count($crows),
            "data"=>$rows->items(),
            "limit"=>$limit
        ];
        return json_encode($data);
    }
    //添加服务介绍
    public function introduce_insert(Request $request){
        $data=$request->post("result");
        $rows=DB::table('index_introduce')->insert($data);
        if($rows){
            return 1;
        }
    }
    //服务介绍回显
    public function introduce_edit($id){
        $rows=DB::table('index_introduce')->where('id',$id)->first();
        return view('admin.introduce.introduce_edit',["rows"=>$rows]);
    }
    //服务介绍回显
    public function introduce_update(Request $request){
        $data=$request->post('result');
        $rows=DB::table('index_introduce')->where('id',$data['id'])->update($data);
        if($rows){
            return 1;
        }
    }
    //删除服务介绍
    public function introduce_del(Request $request){
        $id=$request->get('id');
        $rows=DB::table('index_introduce')->where('id',$id)->delete();
        if($rows){
            return 1;
        }
    }
}